package com.smartlink.util;

import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.smartlink.pojo.Common;

public class HttpClientUtil {

	public JSONObject doPost(String url, JSONObject json) {
		HttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		JSONObject getData = null;
		post.setHeader("api-key", Common.APIKEY);
		try {
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");// 发送json数据需要设置contentType
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);// 返回json格式：
				getData = new JSONObject(result);
				System.out.println(getData.toString());
				return getData;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;

	}

	public JSONObject doPost(HttpPost post) {
		HttpClient client = HttpClients.createDefault();
		JSONObject getData = null;
		// post.setHeader("api-key", Common.APIKEY);
		try {
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);// 返回json格式：
				getData = new JSONObject(result);
				System.out.println(getData.toString());
				return getData;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public JSONObject doGet(String url) {
		HttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(url);
		get.setHeader("api-key", Common.APIKEY);
		JSONObject getData = null;
		try {
			HttpResponse res = client.execute(get);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);// 返回json格式：
				getData = new JSONObject(result);
				System.out.println(getData.toString());
				return getData;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	public boolean doDownload(String url, OutputStream outputStream) {
		HttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(url);
		get.setHeader("api-key", Common.APIKEY);
		boolean flag = false;
		try {
			HttpResponse res = client.execute(get);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				entity.writeTo(outputStream);
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}

	public JSONObject doDelte(String url) {
		HttpClient client = HttpClients.createDefault();
		HttpDelete delete = new HttpDelete(url);
		delete.setHeader("api-key", Common.APIKEY);
		JSONObject getData = null;
		try {
			HttpResponse res = client.execute(delete);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);// 返回json格式：
				getData = new JSONObject(result);
				System.out.println(getData.toString());
				return getData;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}
